package com.alm.handle.service.impl;

import com.alm.handle.core.domain.entity.SysDevInstructionType;
import com.alm.handle.core.domain.entity.SysInstructionCollection;
import com.alm.handle.mapper.SysDevInstructionTypeMapper;
import com.alm.handle.mapper.SysInstructionCollectionMapper;
import com.alm.handle.service.ISysDevUpgradeService;
import com.alm.handle.util.InstrucTionFInal;
import com.alm.handle.util.InstructionByteUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysDevUpgradeServiceImpl implements ISysDevUpgradeService {

    @Autowired
    private SysDevInstructionTypeMapper devInstructionTypeMapper;

    @Autowired
    private SysInstructionCollectionMapper instructionCollectionMapper;

    @Override
    public byte[] getUpgradeData(String deviceId, String url) {
        byte [] configData=url.getBytes();
        byte [] data=new byte[configData.length+2];
        data[0]= (byte) 0xE0;
        data[1]= (byte) configData.length;
        for (int i = 2; i < data.length; i++) {
            data[i]=configData[i-2];
        }
        SysDevInstructionType dev=devInstructionTypeMapper.queryByDevId(deviceId);
        SysInstructionCollection coll=instructionCollectionMapper.selectByPrimaryKey(InstrucTionFInal.ITY_5750);
        ByteBuf befor= InstructionByteUtils.getBFBYTEInstruction(deviceId,
                (short) dev.getDevType().intValue(),(short)0);
        byte[] after=InstructionByteUtils.getBYTEISData(Short.parseShort(coll.getHead(),16),
                Integer.parseInt(dev.getDevInstructionDataType(),16)  ,
                Integer.parseInt(coll.getType(),16),//数据体
                data,Short.parseShort(coll.getLth(),16));
        return InstructionByteUtils.getBYTEISBYTES(befor,after);
    }

    @Override
    public byte[] getVersion(String devid) {
        return new byte[0];
    }
}
